package com.kll.carolinehmibiz.presentation.presenter

import android.content.Context
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import com.kll.carolinehmibiz.OperationManager
import com.kll.carolinehmibiz.base.net.NetCode
import com.kll.carolinehmibiz.model.OperateListener
import com.kll.carolinehmibiz.net.bean.GetFamilyResponse
import com.kll.carolinehmibiz.net.bean.GetRoomIdResponse
import com.kll.carolinehmibiz.net.bean.GetTencentUserIdResponse
import com.kll.carolinehmibiz.presentation.BasePresenter
import com.kll.carolinehmibiz.presentation.listener.MainContactFamilyProtocol

class MainContactFamilyPresenter(context: Context, mView: MainContactFamilyProtocol.IView): BasePresenter(context), MainContactFamilyProtocol.IPresenter {
    private var mView: MainContactFamilyProtocol.IView? = mView
    var getFamilyIsBusying = false
    var getTencentIdIsBusying = false
    var getRoomIdIsBusying = false
    override fun getFamily() {
        if (getFamilyIsBusying) return
        getFamilyIsBusying = true
        OperationManager.getInstancce().getFamily(object : OperateListener {
            override fun onSuccess(state: Int, obj: Any?) {
                getFamilyIsBusying = false
                if (state == NetCode.NET_CODE_OK) {
                    val data = GsonBuilder().create().fromJson<ArrayList<GetFamilyResponse>>(GsonBuilder().create().toJson(obj), object : TypeToken<ArrayList<GetFamilyResponse>>(){}.type)
                    mView?.getFamilySuccessed(data)
                }
            }

            override fun onFail(code: Int, message: String) {
                getFamilyIsBusying = false
                mView?.getFamilyFailed(code, message)
            }

        })
    }

    override fun getTencentId(tencentUserId: String) {
        if (getTencentIdIsBusying) return
        getTencentIdIsBusying = true
        OperationManager.getInstancce().getTencentId(tencentUserId, object : OperateListener {
            override fun onSuccess(state: Int, obj: Any?) {
                getTencentIdIsBusying = false
                if (state == NetCode.NET_CODE_OK) {
                    mView?.getTencentIdSuccessed((obj as GetTencentUserIdResponse).tencent_user_id)
                }
            }

            override fun onFail(code: Int, message: String) {
                getTencentIdIsBusying = false
                mView?.getTencentIdFailed(code, message)
            }

        })
    }

    override fun getRoomId(tencentUserId: String) {
        if (getRoomIdIsBusying) return
        getRoomIdIsBusying = true
        OperationManager.getInstancce().getRoomId(tencentUserId, object : OperateListener {
            override fun onSuccess(state: Int, obj: Any?) {
                getRoomIdIsBusying = false
                if (state == NetCode.NET_CODE_OK) {
                    mView?.getRoomIdSuccess(tencentUserId,(obj as GetRoomIdResponse).roomid)
                }
            }

            override fun onFail(code: Int, message: String) {
                getRoomIdIsBusying = false
                mView?.getRoomIdFailed(code, message)
            }

        })
    }
}